home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 03 - 1987 / 03.01 Jan 87 / pascal source / scrollstuff < prev    next >
Encoding:
Text File  |  1986-11-19  |  5.4 KB  |  228 lines  |  [TEXT/PJMM]

  1. UNIT ScrollStuff;
  2.  
  3. INTERFACE
  4.  
  5.     USES
  6.         ROM85, EditorGlobals;
  7.  
  8.     PROCEDURE AdjustScrollBar (HandleIndex : integer);
  9.     PROCEDURE ScrollChar (HandleIndex : integer;
  10.                                     charPos : integer;
  11.                                     toBottom : boolean);
  12.     PROCEDURE CheckInsertion (HandleIndex : integer);
  13.     PROCEDURE doScrollers (HandleIndex : integer;
  14.                                     scrollBar : ControlHandle;
  15.                                     part : integer;
  16.                                     localMouse : point);
  17.     FUNCTION AutoScroll : boolean;
  18.  
  19. IMPLEMENTATION
  20.  
  21.     FUNCTION Sign (Longvar : Longint) : integer;
  22.     BEGIN
  23.         IF Longvar >= 0 THEN
  24.             sign := 1
  25.         ELSE IF Longvar < 0 THEN
  26.             sign := -1;
  27.     END;
  28.  
  29.     FUNCTION LinesInText (HandleIndex : integer) : integer;
  30. { Fix text edit bug for cr at end of text }
  31.         VAR
  32.             i : integer;
  33.             lines : integer;
  34.             texthandle : CharsHandle;
  35.     BEGIN
  36.         i := HandleIndex;
  37.         WITH myText[i]^^ DO
  38.             BEGIN
  39.                 lines := nLines;
  40.                 texthandle := CharsHandle(hText);
  41.                 IF teLength > 0 THEN
  42.                     IF texthandle^^[teLength - 1] = CR THEN
  43.                         lines := lines + 1;
  44.                 LinesInText := lines
  45.             END
  46.     END;
  47.  
  48.     PROCEDURE MoveText (HandleIndex : integer);
  49.         VAR
  50.             ViewTop, DestTop : LongInt;
  51.             scrollvalue : LongInt;
  52.             height : integer;
  53.             i, j : integer;
  54.             scrolldiff : longInt;
  55.             oldScroll : longInt;
  56.             newScroll : longInt;
  57.     BEGIN
  58.         i := HandleIndex;
  59.         ViewTop := myText[i]^^.ViewRect.top;
  60.         DestTop := myText[i]^^.DestRect.top;
  61.         oldScroll := LongInt(ViewTop - DestTop);
  62.         scrollvalue := GetCtlValue(myVControls[i]);
  63.         height := myText[i]^^.lineHeight;
  64.         newScroll := LongInt(scrollvalue * Height);
  65.         scrolldiff := LongInt(oldscroll - newscroll);
  66.         IF abs(scrolldiff) > 32000 THEN
  67.             BEGIN
  68.                 TEScroll(0, sign(scrolldiff) * 32000, myText[i]);
  69.                 sysbeep(5); {overflow destRect! Text Edit Limitation!}
  70.             END
  71.         ELSE
  72.             BEGIN
  73.                 IF (scrolldiff <> 0) THEN
  74.                     TEScroll(0, scrolldiff, myText[i]); {vertical - see bug TN#22}
  75.             END;
  76.     END;
  77.  
  78.     PROCEDURE scroll_up (theControl : controlHandle;
  79.                                     partCode : integer);
  80.         VAR
  81.             newValue : integer;
  82.     BEGIN
  83.         newValue := GetCtlValue(theControl) - 1;
  84.         SetCtlValue(theControl, newValue);
  85.         moveText(currentWindow);
  86.     END;
  87.  
  88.     PROCEDURE scroll_down (theControl : controlHandle;
  89.                                     partCode : integer);
  90.         VAR
  91.             newValue : integer;
  92.     BEGIN
  93.         newValue := GetCtlValue(theControl) + 1;
  94.         SetCtlValue(theControl, newValue);
  95.         moveText(currentWindow);
  96.     END;
  97.  
  98.     PROCEDURE page_scroll (part : integer;
  99.                                     scrollBar : ControlHandle;
  100.                                     direction : integer);
  101.         VAR
  102.             newValue : integer;
  103.             page : integer;
  104.     BEGIN
  105.         WITH myText[currentWindow]^^, viewRect DO
  106.             page := direction * ((bottom - top) DIV lineHeight - 1);
  107.         newValue := GetCtlValue(scrollBar) + page;
  108.         SetCtlValue(scrollBar, newValue);
  109.         moveText(currentWindow);
  110.     END;
  111.  
  112.     PROCEDURE AdjustScrollBar; {(HandleIndex:integer)}
  113.         VAR
  114.             WindowLInes : integer;
  115.             i : integer;
  116.             currentLines : integer;
  117.     BEGIN
  118.         i := HandleIndex;
  119.         WITH myText[i]^^ DO
  120.             WindowLines := ((ViewRect.bottom - ViewRect.top) DIV lineHeight);
  121.         currentLines := LinesInText(i);
  122.         IF currentLines >= WindowLines THEN
  123.             SetCtlMax(myVControls[i], currentLines - WindowLines)
  124.         ELSE
  125.             SetCtlMax(myVControls[i], 0)
  126.     END;
  127.  
  128.     PROCEDURE ScrollChar; {(HandleIndex : integer;}
  129.                                     {charPos : integer;}
  130.                                     {toBottom : boolean);}
  131.         VAR
  132.             i : integer;
  133.             theLine : integer;
  134.             WindowLines : integer;
  135.     BEGIN
  136.         i := HandleIndex;
  137.         theLine := 0;
  138.         WITH myText[i]^^ DO
  139.             WHILE lineStarts[theLine + 1] <= CharPos DO
  140.                 theLine := theLine + 1;
  141.         IF toBottom THEN
  142.             BEGIN
  143.                 WITH myText[i]^^ DO
  144.                     windowLines := (viewRect.bottom - viewRect.top) DIV lineheight;
  145.                 theLine := theLine - (windowLines - 1);
  146.             END;
  147.         setCtlValue(myVControls[i], theLine);
  148.         MoveText(i);
  149.     END;
  150.  
  151.     PROCEDURE checkInsertion; {(HandleIndex : integer)}
  152.         VAR
  153.             i : integer;
  154.             topline : integer;
  155.             bottomline : integer;
  156.             WindowLines : integer;
  157.  
  158.     BEGIN
  159.         i := HandleIndex;
  160.         WITH myText[i]^^ DO
  161.             WindowLines := (ViewRect.bottom - ViewRect.top) DIV lineHeight;
  162.         topline := GetCtlValue(myVControls[i]);
  163.         bottomline := topLine + WindowLines;
  164.         WITH myText[i]^^ DO
  165.             IF GetCtlMax(myVControls[i]) = 0 THEN
  166.                 MoveText(i)
  167.             ELSE IF selEnd < lineStarts[topLine] THEN
  168.                 ScrollChar(i, selStart, False)
  169.             ELSE IF selStart >= lineStarts[bottomLine] THEN
  170.                 ScrollChar(i, selEnd, true);
  171.     END;
  172.  
  173.     PROCEDURE doScrollers; {HandleIndex:integer}
  174.                                         {(scrollBar : ControlHandle}
  175.                                         {part : integer}
  176.                                         {localMouse : point)}
  177.         VAR
  178.             result : integer;
  179.             i : integer;
  180.     BEGIN
  181.         i := HandleIndex;
  182.         CASE part OF
  183.             inUpButton : 
  184.                 result := TrackControl(scrollbar, localMouse, @scroll_up);
  185.             inDownButton : 
  186.                 result := TrackControl(scrollbar, localMouse, @scroll_down);
  187.             inPageUp : 
  188.                 page_scroll(part, scrollBar, -1);
  189.             inPageDown : 
  190.                 page_scroll(part, scrollBar, 1);
  191.             inThumb : 
  192.                 BEGIN
  193.                     result := TrackControl(scrollBar, localMouse, NIL);
  194.                     moveText(i);
  195.                 END;
  196.             OTHERWISE
  197.                 BEGIN
  198.                 END;
  199.         END; {of case}
  200.     END; {of proc}
  201.  
  202.     FUNCTION AutoScroll; {:boolean;}
  203.         VAR
  204.             result : boolean;
  205.             oldClip : RgnHandle;
  206.             mouseLoc : point;
  207.             TextRect : rect;
  208.             i : integer;
  209.             control : controlHandle;
  210.     BEGIN
  211.         i := currentWindow;
  212.         result := true;
  213.         oldClip := NewRgn;
  214.         GetClip(oldClip);
  215.         ClipRect(myWindows[i]^.portRect);
  216.         GetMouse(mouseLoc);
  217.         TextRect := myText[i]^^.viewRect;
  218.         Control := myVControls[i];
  219.         IF mouseLoc.v < TextRect.top THEN
  220.             Scroll_Up(Control, InUpButton)
  221.         ELSE IF mouseLoc.v > TextRect.bottom THEN
  222.             Scroll_Down(Control, InDownButton);
  223.         SetClip(oldClip);
  224.         DisposeRgn(oldClip);
  225.         AutoScroll := result;
  226.     END;
  227.  
  228. END. {of unit}